/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fwdisp.gui.tabs.settings;

import fwdisp.Main;
import fwdisp.gui.moduls.ColorCells.ColorCellRenderer;
import fwdisp.gui.moduls.IDTable;
import fwdisp.gui.moduls.SecRowPanel;
import fwdisp.gui.moduls.SimpleFileFilter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import javax.swing.table.TableColumn;
import settings.MainSettingsManager;
import java.util.ArrayList;
import settings.collections.ETypeCollection;
import settings.collections.elements.EType;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.awt.event.WindowListener;
import java.io.File;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author rbs
 */
public class EmergencyTypeSettingsPanel extends SecRowPanel {

    public EmergencyTypeSettingsPanel() {
        setName("Einsatztypen");

        setLayout(new BorderLayout(40, 40));

        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        
        

        String names[] = new String[]{"ID", "Einsatztyp", "Alarmtext enthält", "Hintergrundfarbe", "Bild"};

        final DefaultTableModel dtm = new DefaultTableModel(names, 0);

        final IDTable table = new IDTable(dtm);
        TableColumn column = table.getColumnModel().getColumn(3);
        column.setCellRenderer(new ColorCellRenderer());
                
       
        table.addMouseListener(new MouseListener() {
            
            File lastDir = new File("");
            Color col = Color.BLACK;
            
            public void mouseClicked(MouseEvent e) {
                final int row = table.rowAtPoint(e.getPoint());
                final int column = table.columnAtPoint(e.getPoint());
                
                if (column == 3 && table.isEnabled())
                {
                    final JDialog frame = new JDialog();
                   table.setEnabled(false);
                    frame.setMinimumSize(new Dimension(450, 450));
                    
                    JButton addButton = new JButton("Auswählen");

                    final JColorChooser cChooser = new JColorChooser();
                 
                    
                    addButton.addActionListener(new ActionListener() {

                        public void actionPerformed(ActionEvent e) {
                            MainSettingsManager.getSetting().geteTypeCollection().getEType(row).setBack(cChooser.getColor());
                            col = cChooser.getColor();
                            //table.setValueAt(cChooser.getColor().getRGB(), row, column);
                            frame.dispose();
                            table.setEnabled(true);
                        }
                    });
                    frame.setLayout(new BorderLayout());
                    frame.add(cChooser, BorderLayout.CENTER);
                    frame.add(addButton, BorderLayout.SOUTH);
                    frame.setVisible(true);
                    
                    frame.addWindowListener(new WindowListener() {

                        public void windowOpened(WindowEvent e) {}

                        public void windowClosing(WindowEvent e) {
                            table.setEnabled(true);
                        }
                        
                        public void windowClosed(WindowEvent e) {}

                        public void windowIconified(WindowEvent e) {}

                        public void windowDeiconified(WindowEvent e) {}

                        public void windowActivated(WindowEvent e) {}

                        public void windowDeactivated(WindowEvent e) {}
                    });
                }
                else if(column == 4 && table.isEnabled())
                {
                    JFileChooser fileChooser = new JFileChooser(Main.getProgramPath() + File.separator + "pictures");
                    fileChooser.setFileFilter(new SimpleFileFilter("PNG", "*.png", true));
                                        
                    int fileChooserExit = fileChooser.showOpenDialog(Main.getMainframe());
                    if (fileChooserExit == JFileChooser.APPROVE_OPTION)
                    {
                        lastDir = fileChooser.getSelectedFile();
                        MainSettingsManager.getSetting().geteTypeCollection().getEType(row).setPic(lastDir);
                        table.setValueAt(lastDir.getName(), row, column);
                    }
                    else if (fileChooserExit == JFileChooser.CANCEL_OPTION)
                    {
                        MainSettingsManager.getSetting().geteTypeCollection().getEType(row).setPic(new File(""));
                        table.setValueAt("keines", row, column);
                    }
                }
                
            }

            public void mousePressed(MouseEvent e) {}
            public void mouseReleased(MouseEvent e) {}
            public void mouseEntered(MouseEvent e) {}
            public void mouseExited(MouseEvent e) {}
        });
        
        dtm.addTableModelListener(new TableModelListener() {

            public void tableChanged(TableModelEvent tme) {
                
                int row = tme.getFirstRow();
                if (tme.getType() == TableModelEvent.UPDATE)
                {
                    System.out.println("Row edited: " + row
                                    + " Col edited: " + tme.getColumn());
                    ETypeCollection eTypeCollection = MainSettingsManager.getSetting().geteTypeCollection();
                    EType old_eType = eTypeCollection.getEType(row);
                    
                   
                    //Color back = new Color(Integer.parseInt(dtm.getValueAt(row, 3).toString()));
                                        
                    eTypeCollection.getEType(row).setId(row);
                    eTypeCollection.getEType(row).setName((String) dtm.getValueAt(row, 1));
                    eTypeCollection.getEType(row).setContains((String) dtm.getValueAt(row, 2));
                                                                     
                    AusrueckSettingsPanel.updateAusrueckTypes();
                }
            }
        });
        
        //adding types from file:
        ArrayList<EType> eTypes = MainSettingsManager.getSetting().geteTypeCollection().getETypes();
        for (EType type: eTypes)
        {
            String pic_name;
            if (!type.getPic().getPath().equals(""))
                pic_name = type.getPic().getName();
            else
                pic_name = "keines";
            
            String[] row = {"" + type.getId(), type.getName(), type.getContains(), "", pic_name};
            dtm.addRow(row); 
        }
        //AusrueckSettingsPanel.updateETypes();
        
        //table.setEnabled(false);
        add(new JScrollPane(table), BorderLayout.CENTER);

        JPanel new_object = new JPanel(new GridLayout(1, 3, 20, 0));

        JButton add = new JButton("+");
        add.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                ETypeCollection eTypeCollection = MainSettingsManager.getSetting().geteTypeCollection();
                eTypeCollection.addEType(new EType(dtm.getRowCount(), "", "", Color.GRAY, new File("")));
                dtm.addRow(new String[]{"" + dtm.getRowCount(), "", "", "", ""});
                AusrueckSettingsPanel.updateAusrueckTypes();
            }
        });

        JButton sub = new JButton("-");
        sub.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                
                int selRow = table.getSelectedRow();
                if (selRow == -1)
                    if (table.getRowCount() > 0)
                        selRow = table.getRowCount() - 1;
                    else
                        return;
                
               
                for (int i = selRow + 1; i < table.getRowCount(); i++)
                {
                    dtm.setValueAt(i - 1, i, 0);
                }
                
                ETypeCollection eTypeCollection = MainSettingsManager.getSetting().geteTypeCollection();
                eTypeCollection.delEType(selRow);
                dtm.removeRow(selRow);
                AusrueckSettingsPanel.updateAusrueckTypes();
            }
        });

        new_object.add(sub);
        new_object.add(add);
        new_object.setBackground(getBackground());
        add(new_object, BorderLayout.SOUTH);
    }
}
